package com.yx.xinruitu.service.impl;

import com.yx.xinruitu.dao.StaffDao;
import com.yx.xinruitu.entity.PaginationList;
import com.yx.xinruitu.entity.ResponseModel;
import com.yx.xinruitu.service.IStaffService;
import com.yx.xinruitu.util.DateUtil;
import com.yx.xinruitu.util.ParameterMap;
import com.yx.xinruitu.util.Tools;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Service
public class StaffService implements IStaffService{

	@Autowired
	private StaffDao staffDao;

	@Value("${upload.root.folder}")
	public String root_fold;

	@Value("${img.folder}")
	public String img_fold;

	@Value("${user.folder}")
	public String user_folder;

	private Logger log = Logger.getLogger(this.getClass());


	@Override
	@Transactional
	public HashMap<String, Object> getStaffListByPage(ParameterMap pm,Integer pageSize,Integer  currIndex) {
		try {
			pm.put("currIndex",(currIndex-1)*pageSize);
			pm.put("pageSize",pageSize);
			List<ParameterMap> list=staffDao.getStaffListByPage(pm);
			Integer allcount=staffDao.getStaffListCount(pm);
			PaginationList page=new PaginationList();
			page.setList(list);
			page.setDraw(currIndex.longValue());
			page.setPageSize(pageSize);
			page.setRecordsTotal(allcount!=null?allcount:0);
			page.setTotalpage(page.getRecordsTotal() % pageSize > 0 ? (page.getRecordsTotal() / pageSize) + 1 : (page.getRecordsTotal() / pageSize));
			return ResponseModel.getModel("ok", "success",page);
		}catch(Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			e.printStackTrace();
			log.error("add menu error", e);
			return ResponseModel.getModel("error", "falied", null);
		}
	}


	@Override
	@Transactional
	public HashMap<String, Object> add(ParameterMap pm) {
		try {
			String phone = pm.getString("phone");
			if(Tools.isEmpty(pm.getString("phone")) || Tools.isEmpty(phone)){
				return ResponseModel.getModel("你请求的是冒牌接口", "falied", null);
			}
			ParameterMap staffpm = staffDao.findStaffByPhone(pm);
			if(staffpm != null){
				return ResponseModel.getModel("您使用的手机已注册，请更换！", "falied", null);
			}
			pm.put("register_time",DateUtil.getTime());
			staffDao.saveStaff(pm);
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			e.printStackTrace();
			log.error("error:"+e.getMessage(), e);
			return ResponseModel.getModel("提交失败", "failed", null);
		}
		return ResponseModel.getModel("ok", "success", null);
	}


	@Override
	@Transactional
	public HashMap<String, Object> del(ParameterMap pm) {
		try {
			String staffId = pm.getString("id");
			if(Tools.isEmpty(staffId)){
				return ResponseModel.getModel("你请求的是冒牌接口", "falied", null);
			}
			staffDao.delStaff(staffId);
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			e.printStackTrace();
			log.error("error:"+e.getMessage(), e);
			return ResponseModel.getModel("提交失败", "failed", null);
		}
		return ResponseModel.getModel("ok", "success", null);
	}

	@Override
	@Transactional
	public HashMap<String, Object> editPwd(ParameterMap pm) {
		try {
			String phone = pm.getString("phone");
			if(Tools.isEmpty(pm.getString("phone")) || Tools.isEmpty(phone)){
				return ResponseModel.getModel("你请求的是冒牌接口", "falied", null);
			}
			ParameterMap staffpm = staffDao.findStaffByPhone(pm);
			if(staffpm != null){
				pm.put("id",staffpm.get("id").toString());
				staffDao.editStaff(pm);
				return ResponseModel.getModel("密码修改成功！", "success", null);
			}else{
				return ResponseModel.getModel("请输入正确的姓名及手机号，找回密码！", "falied", null);
			}
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			e.printStackTrace();
			log.error("error:"+e.getMessage(), e);
			return ResponseModel.getModel("提交失败", "failed", null);
		}
	}

	@Override
	@Transactional
	public HashMap<String, Object> find(ParameterMap pm,HttpSession session) {
		try {
			String phone = pm.getString("phone");
			if(Tools.isEmpty(phone)){
				return ResponseModel.getModel("你请求的是冒牌接口", "falied", null);
			}
			ParameterMap map=staffDao.findStaffByPhone(pm);

			if(map!=null){
				Object exam_time=map.get("exam_time");
				if(exam_time!=null&&!exam_time.equals("")){
					//exam_time!=null 为已答过题继续答题，判断时间确定今天是否答过
					String exam_day=exam_time.toString().substring(0,10);
					String curday= DateUtil.getDay();
					if(exam_day.equals(curday)){
						return ResponseModel.getModel("今天已提交，请明天继续答题！", "falied", null);
					}else{

						session.setAttribute("staffid",map.get("id").toString());
						return ResponseModel.getModel("ok", "success",map);
					}
				}else{
					//exam_time==null 为新注册还未答题
					session.setAttribute("staffid",map.get("id").toString());
					return ResponseModel.getModel("ok", "success",map);
				}
			}else{
				return ResponseModel.getModel("请输入正确的手机号及密码！", "falied", null);
			}
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			e.printStackTrace();
			log.error("error:"+e.getMessage(), e);
			return ResponseModel.getModel("提交失败", "failed", null);
		}
	}

	@Override
	@Transactional
	public HashMap<String, Object> examCommit(ParameterMap pm) {
		try {
			String staffid = pm.getString("staffid");
			if(Tools.isEmpty(pm.getString("staffid")) || Tools.isEmpty(staffid)){
				return ResponseModel.getModel("你请求的是冒牌接口", "falied", null);
			}

			ParameterMap map=staffDao.findStaff(staffid);
			String answerType = (String) pm.get("answerType");
			double oldscore=0.0;
			if (answerType.equals("1")){
				oldscore=new Double(map.get("score").toString());
			}
			if (answerType.equals("2")){
				oldscore=new Double(map.get("fs_score").toString());
			}
			double  score=new Double(pm.get("score").toString());
			pm.put("type", answerType);
			map.put("userTime",pm.getString("use_time"));
			if(score>oldscore){
				pm.put("exam_time",DateUtil.getTime());
				pm.put("dept",map.get("company"));
				if (answerType.equals("1")||answerType.equals("2")) {
					staffDao.saveRecord(pm);

					if (answerType.equals("1")) {
						staffDao.examCommit(pm);
					}
					if (answerType.equals("2")) {
						staffDao.fs_examCommit(pm);
					}
				}
				map.put("maxscore",score);
			}else{
				pm.put("exam_time",DateUtil.getTime());
				pm.put("dept",map.get("company"));
				if (answerType.equals("1")||answerType.equals("2")) {
					staffDao.saveRecord(pm);
				}
				pm.put("score",null);
				pm.put("use_time",null);
				if (answerType.equals("1")||answerType.equals("2")) {
					if (answerType.equals("1")) {
						staffDao.examCommit(pm);
					}
					if (answerType.equals("2")) {
						staffDao.fs_examCommit(pm);
					}
				}
				map.put("score",score);
				map.put("maxscore",oldscore);
			}

			map.put("curscore",score);
			return ResponseModel.getModel("ok", "success", map);
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			e.printStackTrace();
			log.error("error:"+e.getMessage(), e);
			return ResponseModel.getModel("提交失败", "failed", null);
		}
	}

	@Override
	@Transactional
	public HashMap<String, Object> getOrderList(ParameterMap pm, Integer pageSize, Integer currIndex) {
		try {
			return ResponseModel.getModel("ok", "success",staffDao.findOrderList(pm));
		}catch(Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			e.printStackTrace();
			log.error("add menu error", e);
			return ResponseModel.getModel("error", "falied", null);
		}
	}
	@Override
	@Transactional
	public HashMap<String, Object> csList(ParameterMap pm, Integer pageSize, Integer currIndex) {
		try {
			pm.put("currIndex",(currIndex-1)*pageSize);
			pm.put("pageSize",pageSize);
			List<ParameterMap> list=staffDao.cslist(pm);
			Integer allcount=staffDao.csAllCount(pm);
			PaginationList page=new PaginationList();
			page.setList(list);
			page.setDraw(currIndex.longValue());
			page.setPageSize(pageSize);
			page.setRecordsTotal(allcount!=null?allcount:0);
			page.setTotalpage(page.getRecordsTotal() % pageSize > 0 ? (page.getRecordsTotal() / pageSize) + 1 : (page.getRecordsTotal() / pageSize));
			return ResponseModel.getModel("ok", "success",page);
		}catch(Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			e.printStackTrace();
			log.error("add menu error", e);
			return ResponseModel.getModel("error", "falied", null);
		}
	}

	@Override
	@Transactional
	public HashMap<String, Object> csDeptlist(ParameterMap pm, Integer pageSize, Integer currIndex) {
		try {
			pm.put("currIndex",(currIndex-1)*pageSize);
			pm.put("pageSize",pageSize);
			List<ParameterMap> list=staffDao.csDeptlist(pm);
			Integer allcount=staffDao.csDeptAllCount(pm);
			PaginationList page=new PaginationList();
			page.setList(list);
			page.setDraw(currIndex.longValue());
			page.setPageSize(pageSize);
			page.setRecordsTotal(allcount!=null?allcount:0);
			page.setTotalpage(page.getRecordsTotal() % pageSize > 0 ? (page.getRecordsTotal() / pageSize) + 1 : (page.getRecordsTotal() / pageSize));
			return ResponseModel.getModel("ok", "success",page);
		}catch(Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			e.printStackTrace();
			log.error("add menu error", e);
			return ResponseModel.getModel("error", "falied", null);
		}
	}

	@Override
	@Transactional
	public HashMap<String, Object> fsList(ParameterMap pm, Integer pageSize, Integer currIndex) {
		try {
			pm.put("currIndex",(currIndex-1)*pageSize);
			pm.put("pageSize",pageSize);
			List<ParameterMap> list=staffDao.fslist(pm);
			Integer allcount=staffDao.fsAllCount(pm);
			PaginationList page=new PaginationList();
			page.setList(list);
			page.setDraw(currIndex.longValue());
			page.setPageSize(pageSize);
			page.setRecordsTotal(allcount!=null?allcount:0);
			page.setTotalpage(page.getRecordsTotal() % pageSize > 0 ? (page.getRecordsTotal() / pageSize) + 1 : (page.getRecordsTotal() / pageSize));
			return ResponseModel.getModel("ok", "success",page);
		}catch(Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			e.printStackTrace();
			log.error("add menu error", e);
			return ResponseModel.getModel("error", "falied", null);
		}
	}
	@Override
	@Transactional
	public HashMap<String, Object> fsDeptlist(ParameterMap pm, Integer pageSize, Integer currIndex) {
		try {
			pm.put("currIndex",(currIndex-1)*pageSize);
			pm.put("pageSize",pageSize);
			List<ParameterMap> list=staffDao.fsDeptlist(pm);
			Integer allcount=staffDao.fsAllDeptCount(pm);
			PaginationList page=new PaginationList();
			page.setList(list);
			page.setDraw(currIndex.longValue());
			page.setPageSize(pageSize);
			page.setRecordsTotal(allcount!=null?allcount:0);
			page.setTotalpage(page.getRecordsTotal() % pageSize > 0 ? (page.getRecordsTotal() / pageSize) + 1 : (page.getRecordsTotal() / pageSize));
			return ResponseModel.getModel("ok", "success",page);
		}catch(Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			e.printStackTrace();
			log.error("add menu error", e);
			return ResponseModel.getModel("error", "falied", null);
		}
	}
	@Override
	@Transactional
	public HashMap<String, Object> fs_Deptlist(ParameterMap pm, Integer pageSize, Integer currIndex) {
		try {
			pm.put("currIndex",(currIndex-1)*pageSize);
			pm.put("pageSize",pageSize);
			List<ParameterMap> list=staffDao.fs_Deptlist(pm);
			Integer allcount=staffDao.fs_DeptAllCount(pm);
			PaginationList page=new PaginationList();
			page.setList(list);
			page.setDraw(currIndex.longValue());
			page.setPageSize(pageSize);
			page.setRecordsTotal(allcount!=null?allcount:0);
			page.setTotalpage(page.getRecordsTotal() % pageSize > 0 ? (page.getRecordsTotal() / pageSize) + 1 : (page.getRecordsTotal() / pageSize));
			return ResponseModel.getModel("ok", "success",page);
		}catch(Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			e.printStackTrace();
			log.error("add menu error", e);
			return ResponseModel.getModel("error", "falied", null);
		}
	}
	@Override
	@Transactional
	public HashMap<String, Object> getdeptList(ParameterMap pm) {
		try {
			return ResponseModel.getModel("ok", "success", staffDao.deptlist(pm));
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			e.printStackTrace();
			log.error("error:"+e.getMessage(), e);
			return ResponseModel.getModel("提交失败", "failed", null);
		}
	}
}
